﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace WindowsGameLibrary1
{
    public class Collision
    {
        public static void CheckCollision(List<Entity> entitys)
        {
            int winner = 0;
            for (int i = 0; i < entitys.Count; i++)
            {
                for (int j = i + 1; j < entitys.Count; j++)
                {
                    if (Check_Collision(entitys[i], entitys[j],out winner))
                    {
                        // La entidad con el typo de colision mas bajo ejecuta su metodo de collisionAccion, el cual debe favorecer a la entidad
                        // pasada por parametro.
                        if (winner==1)
                        {
                            entitys[j].CollisionAction(entitys[i]);
                        }
                        if (winner==2)
                        {
                            entitys[i].CollisionAction(entitys[j]);    
                        }
                        
                        
                    }                    
                    
                }

            }
        }

        private static Boolean Check_Collision(Entity entity1, Entity entity2, out int winner)
        {
            
            BoundingBox box1 = entity1.CollisionRectangle();

            BoundingBox box2 = entity2.CollisionRectangle();
            bool intersects = false;
            ObjectCollisionType bigger1 = ObjectCollisionType.VeryWeak;
            ObjectCollisionType bigger2 = ObjectCollisionType.VeryWeak;

            if (box1.Intersects(box2)) 
            {
                DetailedCollision(entity1, entity2, ref intersects, ref bigger1, ref bigger2);
                winner = MaxType(bigger1, bigger2);
                if (winner!=0)
                {
                    return intersects;
                }
                else
                {
                    return false;
                }
                
            }
            else
            {
                winner = 0;
                return false;
            }
                       
        }

        private static void DetailedCollision(Entity entity1, Entity entity2, ref bool intersects, ref ObjectCollisionType bigger1, ref ObjectCollisionType bigger2)
        {
            List<CollisionCoordinates> boxList1 = entity1.CollisionRectangles();
            List<CollisionCoordinates> boxList2 = entity2.CollisionRectangles();

            for (int i = 0; i < boxList1.Count; i++)
            {
                for (int j = 0; j < boxList2.Count; j++)
                {

                    if (GetBoundingBox(boxList1[i], entity1.Position()).Intersects(GetBoundingBox(boxList2[j], entity2.Position())))
                    {
                        intersects = true;
                        if (bigger1 < boxList1[i].Type)
                        {
                            bigger1 = boxList1[i].Type;
                        }
                        if (bigger2 < boxList2[i].Type)
                        {
                            bigger2 = boxList2[i].Type;
                        }

                    }
                }
            }
        }

        private static int MaxType(ObjectCollisionType bigger1, ObjectCollisionType bigger2)
        {
            int winner;
            if (bigger1 > bigger2)
            {
                winner = 1;
            }
            else
            {
                if (bigger1 > bigger2)
                {
                    winner = 2;
                }
                else
                {
                    winner = 0;
                }
                
            }
            return winner;
        }

        private static BoundingBox GetBoundingBox(CollisionCoordinates collisionCoordinates, Vector2 ownerPosition)
        {
            Vector3 min = new Vector3(ownerPosition.X+collisionCoordinates.Minimus.X, ownerPosition.Y+collisionCoordinates.Minimus.Y, 0);

            Vector3 max = new Vector3(ownerPosition.X + collisionCoordinates.Maximus.X, ownerPosition.Y + collisionCoordinates.Maximus.Y, 0);

            return new BoundingBox(min, max);
        }

    }
}
